package cn.org.book.shop.web.controller.article;

import cn.org.book.shop.article.domain.ApArticle;
import cn.org.book.shop.article.domain.ApArticleDataConfiguration;
import cn.org.book.shop.article.service.IApArticleDataConfigurationService;
import cn.org.book.shop.article.service.IApArticleService;
import cn.org.book.shop.common.annotation.Log;
import cn.org.book.shop.common.core.controller.BaseController;
import cn.org.book.shop.common.core.domain.AjaxResult;
import cn.org.book.shop.common.core.page.TableDataInfo;
import cn.org.book.shop.common.enums.BusinessType;
import cn.org.book.shop.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 文章数据配置Controller
 *
 * @author wanghongjie
 * @date 2020-11-30
 */
@RestController
@RequestMapping("/ap/configuration")
public class ApArticleDataConfigurationController extends BaseController {
    @Autowired
    private IApArticleDataConfigurationService apArticleDataConfigurationService;

    @Autowired
    private IApArticleService apArticleService;

    /**
     * 查询文章数据配置列表
     */
    @PreAuthorize("@ss.hasPermi('ap:configuration:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApArticleDataConfiguration apArticleDataConfiguration) {
        startPage();
        List<ApArticleDataConfiguration> list = apArticleDataConfigurationService.selectApArticleDataConfigurationList(apArticleDataConfiguration);
        return getDataTable(list, apArticleDataConfigurationService.selectTotal(apArticleDataConfiguration));
    }

    /**
     * 导出文章数据配置列表
     */
    @PreAuthorize("@ss.hasPermi('ap:configuration:export')")
    @Log(title = "文章数据配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ApArticleDataConfiguration apArticleDataConfiguration) {
        List<ApArticleDataConfiguration> list = apArticleDataConfigurationService.selectApArticleDataConfigurationList(apArticleDataConfiguration);
        ExcelUtil<ApArticleDataConfiguration> util = new ExcelUtil<>(ApArticleDataConfiguration.class);
        return util.exportExcel(list, "configuration");
    }

    /**
     * 获取文章数据配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:configuration:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(apArticleDataConfigurationService.selectApArticleDataConfigurationById(id));
    }

    /**
     * 新增文章数据配置
     */
    @PreAuthorize("@ss.hasPermi('ap:configuration:add')")
    @Log(title = "文章数据配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApArticleDataConfiguration apArticleDataConfiguration) {
        apArticleDataConfiguration.setArticleName(apArticleService.selectApArticleById(apArticleDataConfiguration.getArticleId()).getTitle());
        return toAjax(apArticleDataConfigurationService.insertApArticleDataConfiguration(apArticleDataConfiguration));
    }

    /**
     * 修改文章数据配置
     */
    @PreAuthorize("@ss.hasPermi('ap:configuration:edit')")
    @Log(title = "文章数据配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApArticleDataConfiguration apArticleDataConfiguration) {
        apArticleDataConfiguration.setArticleName(apArticleService.selectApArticleById(apArticleDataConfiguration.getArticleId()).getTitle());
        return toAjax(apArticleDataConfigurationService.updateApArticleDataConfiguration(apArticleDataConfiguration));
    }

    /**
     * 删除文章数据配置
     */
    @PreAuthorize("@ss.hasPermi('ap:configuration:remove')")
    @Log(title = "文章数据配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(apArticleDataConfigurationService.deleteApArticleDataConfigurationByIds(ids));
    }

    /**
     * 查询未配置的绘本集合
     *
     * @return 未配置的绘本集合
     */
    @GetMapping("/apArticleList")
    public AjaxResult getApArticleList() {
        List<ApArticle> apArticles = apArticleService.selectApArticleList(null);
        List<ApArticleDataConfiguration> apArticleDataConfigurations = apArticleDataConfigurationService.selectApArticleDataConfigurationList(null);
        List<ApArticle> newApArticles = apArticles.stream().filter(item -> eq(apArticleDataConfigurations, item)).collect(Collectors.toList());
        return AjaxResult.success(newApArticles);
    }

    private boolean eq(List<ApArticleDataConfiguration> apArticleDataConfigurations, ApArticle apArticles) {
        AtomicBoolean res = new AtomicBoolean(true);
        apArticleDataConfigurations.forEach(ap -> {
            if (ap.getArticleId().equals(apArticles.getId())) {
                res.set(false);
            }
        });
        return res.get();
    }

}
